home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6528 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Function Prototype placement in source files
  5. Date: Mon, 19 Feb 1996 15:38:59 +0200
  6. Organization: Carelcomp Forest
  7. Message-ID: <31287D73.5B18@cmt.lpr.mail.carel.fi>
  8. References: <4g0obg$78b@barad-dur.nas.com>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  14.  
  15. Matt Scott wrote:
  16. > I have a question of style for everyone knowledgable and interested
  17. > in the subject.
  18. > My C Programming prof. at the community college, as well as the book
  19. > he teaches with, put function prototypes inside main(). I didn't write
  20. > it down or print it out, but here is what I remember of an example he
  21. > wrote in the past to illustrate functions and static variables:
  22. > [snip]
  23. > In almost every instance of prototypes I've seen, they are declared
  24. > outside of any function -- mostly above all other functions, and in
  25. > a header file most often.
  26. > Is there a good reason to do it the way he and the book do it?
  27. > Thanks in advance for any thoughtful answers.
  28.  
  29. Placing function prototypes inside a function body was once a common practice, 
  30. but now when there are standard header files, this should be avoided. Functions 
  31. declared inside a function body are said to have a 'function scope', ie. they 
  32. are properly accessible from within the function declaring them only. 
  33. Furthermore, in your example, you had to declare 'float one(void)' twice, once 
  34. in 'main()' and then in 'two()'. This increases probability of errors. Consider, 
  35. what would happen in a medium-size commercial product program, using strcpy, 
  36. malloc, etc all over the functions. Declaring prototypes in function scope would 
  37. be a very poor practice... And, what if your function 'one()' changed from 
  38. 'float one(void)' to 'double one(int)' ? You'd have to go over your whole bunch 
  39. of functions in order to make it work... Too much work involved, it seems. Just 
  40. stick those prototypes in a header file and #include it. Maybe your professor 
  41. hasn't quite got it with C... :)
  42.  
  43. Later,
  44.  AriL
  45. -- 
  46. All my opinions are mine and mine alone.
  47.